package com.seele.tigerwallet.ui.fragment;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.seele.base.listener.OnMultiClickListener;
import com.seele.tigerwallet.BuildConfig;
import com.seele.tigerwallet.R;
import com.seele.tigerwallet.api.APIException;
import com.seele.tigerwallet.api.APIModule;
import com.seele.tigerwallet.api.APIObjectCallBack;
import com.seele.tigerwallet.api.APIStringCallBack;
import com.seele.tigerwallet.model.LoginHost;
import com.seele.tigerwallet.model.UserInfo;
import com.seele.tigerwallet.model.VersionModel;
import com.seele.tigerwallet.model.WebModel;
import com.seele.tigerwallet.ui.MainActivity;
import com.seele.tigerwallet.ui.RegisterActivity;
import com.seele.tigerwallet.ui.activtiy.AboutActivity;
import com.seele.tigerwallet.ui.activtiy.ContactsActivity;
import com.seele.tigerwallet.ui.activtiy.ManageWalletActivity;
import com.seele.tigerwallet.ui.activtiy.PersonalAuthenticationActivity;
import com.seele.tigerwallet.ui.activtiy.SystemSetActivtiy;
import com.seele.tigerwallet.ui.activtiy.WebViewActivtiy;
import com.seele.tigerwallet.ui.base.BaseFragment;
import com.seele.tigerwallet.ui.popupwindow.UpdatePopupWindow;
import com.seele.tigerwallet.ui.views.LinearRelativeLayout;
import com.seele.tigerwallet.utils.SeeleFileMap;
import com.seele.tigerwallet.utils.Utils;

//import junit.runner.Version;

import java.io.File;

import retrofit2.Call;


/**
 * @Author: 郑波
 * @Time: 2018/6/7 19:57
 * @Description: 我的界面
 */
public class MyFragment extends BaseFragment {

    private static MyFragment myFragment;
    private LinearRelativeLayout mManageWalletRv;
    private LinearRelativeLayout mMyContactsRv;
    private LinearRelativeLayout mSystemSetupRv;
    private LinearRelativeLayout mAuthenticationRv;
    private LinearRelativeLayout mHelpDeskRvRv;
    private LinearRelativeLayout mHardwareWalletRv;
    private LinearRelativeLayout mCheckUpdateRv;
    private ImageView mAddIcon;
    private TextView mTitle;
    private VersionModel mVersionModel;
    private ImageView mNewVersion;//iv_new_version
    private TextView mTvVersion;//tv_version
    private ImageView mImageView;
    private int flag = 0;

    public static MyFragment newInstanec() {
        if (myFragment == null) {
            myFragment = new MyFragment();
        }

        return myFragment;

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        mTitle = $f(R.id.title_tv);
        mTitle.setText(getString(R.string.main_my));
        mAddIcon = $f(R.id.add_icon);
        mAddIcon.setVisibility(View.GONE);
        mManageWalletRv = $f(R.id.manage_wallet_rv);
        mMyContactsRv = $f(R.id.mycontacts_rv);
        mSystemSetupRv = $f(R.id.system_setup_rv);
        mAuthenticationRv = $f(R.id.authentication_rv);
        mHelpDeskRvRv = $f(R.id.help_desk_rv);
        mHardwareWalletRv = $f(R.id.hardware_wallet_rv);
        mNewVersion = $f(R.id.iv_new_version);
        mTvVersion = $f(R.id.tv_version);
        mCheckUpdateRv = $f(R.id.check_update_rv);

        mTvVersion.setText(String.format("V%s", BuildConfig.VERSION_NAME));
        mManageWalletRv.setOnClickListener(onMultiClickListener);
        mMyContactsRv.setOnClickListener(onMultiClickListener);
        mSystemSetupRv.setOnClickListener(onMultiClickListener);
        mAuthenticationRv.setOnClickListener(onMultiClickListener);
        mHelpDeskRvRv.setOnClickListener(onMultiClickListener);
        mHardwareWalletRv.setOnClickListener(onMultiClickListener);
        mCheckUpdateRv.setOnClickListener(onMultiClickListener);

        $f(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag==10){
                    Toast.makeText(mContext,"uuid::"+UserInfo.get().uuid+"::session:"+UserInfo.get().session,Toast.LENGTH_SHORT).show();
                    flag=0;
                }
                flag++;
            }
        });
    }


    OnMultiClickListener onMultiClickListener = new OnMultiClickListener() {
        @Override
        public void onMultiClick(View view) {

            switch (view.getId()) {
                case R.id.manage_wallet_rv://钱包和管理
                    startActivity(ManageWalletActivity.class);
                    break;
                case R.id.mycontacts_rv://联系人
                    startActivity(ContactsActivity.class);
                    break;
                case R.id.system_setup_rv://系统设置
                    startActivity(SystemSetActivtiy.class);
                    break;
                case R.id.authentication_rv://个人认证
                    Toast.makeText(mContext, "正在开发中", Toast.LENGTH_SHORT).show();
//                startActivity(PersonalAuthenticationActivity.class);
                    break;
                case R.id.help_desk_rv://帮助中心
                    WebModel webModel = new WebModel();
                    webModel.title = getResources().getString(R.string.help_desk);
                    String host = UserInfo.get().loginhost;
                    if (!TextUtils.isEmpty(host)) {
                        String url = "http://" + host + "/wallet/v1/help/";
                        webModel.url = url;
                    }

//                webModel.url = "http://106.75.171.117:7001/wallet/v1/help/";

//                webModel.url = "http://106.75.80.49:8000/wallet/v1/help/";
                    webModel.isDisplay = true;
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(WebViewActivtiy.class.getSimpleName(), webModel);
                    startActivity(WebViewActivtiy.class, bundle);

//                Toast.makeText(mContext,"正在开发中",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.hardware_wallet_rv://硬件钱包
                    Toast.makeText(mContext, "正在开发中", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.check_update_rv://检查更新
                    startActivity(AboutActivity.class);

                /*if (mVersionModel == null || mNewVersion.getVisibility() == View.GONE) {
                    return;
                }
                final UpdatePopupWindow updatePopupWindow = new UpdatePopupWindow(mContext);
                updatePopupWindow.setTitle(String.format("有新的版本V%s", mVersionModel.ver));
                updatePopupWindow.setContent(mVersionModel.info);
                updatePopupWindow.setmOnClickListener(new UpdatePopupWindow.OnClickListener() {
                    @Override
                    public void onCancel() {
                        updatePopupWindow.dismiss();
                    }

                    @Override
                    public void onUpdate() {
                        Utils.downLoadAPK(mContext, mVersionModel.downloadurl, mVersionModel.ver);
                        updatePopupWindow.dismiss();
                        Toast.makeText(mContext, "后台下载中.", Toast.LENGTH_LONG).show();
                    }
                });
                updatePopupWindow.showAtLocation(MyFragment.this.getActivity());*/
                    break;
            }

        }
    };



    private void checkVersion() {
        String currentVersion = BuildConfig.VERSION_NAME;
        APIModule.
                getApi()
                .getversion(currentVersion)
                .enqueue(new APIObjectCallBack<VersionModel>() {
                    @Override
                    protected void onResponse(VersionModel response) {
                        mVersionModel = response;
                        int ret = Utils.versionCompare(BuildConfig.VERSION_NAME, mVersionModel.ver);
                        if (ret == 2) {
                            mNewVersion.setVisibility(View.VISIBLE);
                            mTvVersion.setText(String.format("V%s", mVersionModel.ver));
                        } else {
                            mNewVersion.setVisibility(View.GONE);
                        }
                        mVersionModel.info = new String(Base64.decode(response.info.getBytes(), Base64.DEFAULT));
                    }

                    @Override
                    protected void onResponseError(APIException e) {
                    }

                    @Override
                    protected void onFailureError(Call call, Throwable t) {
                    }
                });
    }

}
